#! /bin/sh

cleanup () {
  erl -pa ../src -sname genfrag_killah -eval '
    "genfrag_killah@" ++ Host = atom_to_list (node ()),
    Other = list_to_atom ("genfrag_test1@" ++ Host),
    MoreOther = list_to_atom ("genfrag_test2@" ++ Host),

    rpc:call (Other, erlang, halt, []),
    rpc:call (MoreOther, erlang, halt, [])
  ' -noshell -noinput -s erlang halt 
  wait
}

rm -rf Mnesia*

{
erl -pa ../src -sname genfrag_test1 -s mnesia -eval '
  mnesia:change_table_copy_type (schema, node (), disc_copies),
  timer:sleep (30000)'  \
  -noshell -noinput -s erlang halt &

erl -pa ../src -sname genfrag_test2 -s mnesia -eval '
  "genfrag_test2@" ++ Host = atom_to_list (node ()),
  Self = node (),
  Other = list_to_atom ("genfrag_test1@" ++ Host),
  mnesia:change_config (extra_db_nodes, [ Other ]),
  mnesia:change_table_copy_type (schema, node (), disc_copies),
  timer:sleep (30000)'  \
  -noshell -noinput -s erlang halt &

erl -pa ../src -sname genfrag_test3 -s mnesia -eval '
  "genfrag_test3@" ++ Host = atom_to_list (node ()),
  Self = node (),
  Other = list_to_atom ("genfrag_test1@" ++ Host),
  MoreOther = list_to_atom ("genfrag_test2@" ++ Host),
  mnesia:change_config (extra_db_nodes, [ Other, MoreOther ]),
  mnesia:change_table_copy_type (schema, node (), disc_copies),
  timer:sleep (30000)'  \
  -noshell -noinput -s erlang halt &

sleep 1

erl -pa ../src -sname genfrag_test4 -s mnesia -eval '
  "genfrag_test4@" ++ Host = atom_to_list (node ()),
  Self = node (),
  Other = list_to_atom ("genfrag_test1@" ++ Host),
  MoreOther = list_to_atom ("genfrag_test2@" ++ Host),
  MoreMoreOther = list_to_atom ("genfrag_test3@" ++ Host),
  pong = net_adm:ping (Other),
  pong = net_adm:ping (MoreOther),
  pong = net_adm:ping (MoreMoreOther),
  { ok, Nodes } = mnesia:change_config (extra_db_nodes, [ Other,
                                                          MoreOther,
                                                          MoreMoreOther ]),
  mnesia:change_table_copy_type (schema, node (), disc_copies),
  timer:sleep (1000),

  [ Other, MoreOther, MoreMoreOther ] = lists:sort (erlang:nodes ()),
  [ Other, MoreOther, MoreMoreOther, Self ] = 
    lists:sort (mnesia:system_info (running_db_nodes)),

  { ok, Pid } = gen_fragment:start_link (testdb, false, [ flass, 4, 3 ], []),

  [ [ Other, MoreOther, MoreMoreOther ], 
    [ Other, MoreOther, Self ], 
    [ Other, MoreMoreOther, Self ], 
    [ MoreOther, MoreMoreOther, Self ] ] =
    lists:sort ([
      lists:sort (mnesia:table_info (flass, active_replicas)),
      lists:sort (mnesia:table_info (flass_frag2, active_replicas)),
      lists:sort (mnesia:table_info (flass_frag3, active_replicas)),
      lists:sort (mnesia:table_info (flass_frag4, active_replicas))
      ]),

  rpc:call (MoreOther, erlang, halt, []),
  rpc:call (MoreMoreOther, erlang, halt, []),

  receive after 1000 -> ok end,

  [ Other ] = lists:sort (erlang:nodes ()),
  [ Other, Self ] = lists:sort (mnesia:system_info (running_db_nodes)),

  [ [ Other, Self ], [ Other, Self ], [ Other, Self ], [ Other, Self ] ] =
    lists:sort ([
      lists:sort (mnesia:table_info (flass, active_replicas)),
      lists:sort (mnesia:table_info (flass_frag2, active_replicas)),
      lists:sort (mnesia:table_info (flass_frag3, active_replicas)),
      lists:sort (mnesia:table_info (flass_frag4, active_replicas))
      ])
' -noshell -noinput -s erlang halt || { cleanup; exit 1; }
} > genfrag-deadnode.output 2>&1

cleanup

rm -rf Mnesia*

exit 0
